package com.hundsun.network.uc.controller;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.hundsun.network.uc.common.EWCodeUtil;
import com.hundsun.network.uc.common.ServerUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by zhengkk19738 on 2018/5/2.
 */

@Controller
@RequestMapping("/")
public class QRController {

    private Logger logger = LoggerFactory.getLogger(QRController.class);

    /**
     * @param wsSessionId websocket 会话id （通知页面登录成功的连接）
     * @param request
     * @param response
     * @Title:获取登录二维码图片
     * @date 2018年5月2日16:41:06
     */
    @RequestMapping("getQrCodeImg")
    public void getQrCodeImg(String wsSessionId, HttpServletRequest request, HttpServletResponse response) {

        ServletOutputStream outputStream = null;
        // http://www.lhzsports.com:80/LoginMS/qrLogin?appid=?&json={"data":[{ED:"rigour2046"}]}
        try {
            String serverUrl = ServerUtil.getServerUrl(request);
            String qrLoginUrl = serverUrl + "/qrLogin";// http://192.168.1.102:8080/ssoServer/auth/qrCodeLogin
            qrLoginUrl += "?appid=?";
            String clientId = request.getSession().getId() + ":" + wsSessionId;
            qrLoginUrl += "&json={\"data\":[{ED:\"" + clientId + "\"}]}";// 生成的二维码有data，但是APP发送验证的时候没有data这一层，很尴尬
            // http://192.168.1.102:8080/ssoServer/auth/qrCodeLogin?appid=?&json={"data":[{ED:"B49FBCF0B12A06899BF338F9ACE0CC52"}]}
            logger.info("扫码登录地址：" + qrLoginUrl);
            // 内容所使用编码(有中文则必须指定编码)
            Map<EncodeHintType, String> hints = new HashMap<EncodeHintType, String>();
            hints.put(EncodeHintType.CHARACTER_SET, "UTF8");
            try {
                BitMatrix bitMatrix = new MultiFormatWriter().encode(qrLoginUrl, BarcodeFormat.QR_CODE, 180, 180,
                        hints);
                outputStream = response.getOutputStream();
                EWCodeUtil.writeToStream(bitMatrix, "jpg", outputStream);
                outputStream.flush();
                outputStream.close();
            } catch (WriterException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }


}
